package com.subframe7536.sa.Aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;

/**
 * @author subframe7536
 */
@Aspect
@Slf4j
@Component
@ControllerAdvice
public class LogProcession {

    @AfterReturning(returning = "result", pointcut = "@annotation(com.subframe7536.sa.Aspect.Log)")
    public void test(JoinPoint joinPoint, Object result) {
        log.debug(String.valueOf(joinPoint.getSignature()));
        log.debug(result.toString());
    }

    @AfterThrowing(pointcut = "@annotation(com.subframe7536.sa.Aspect.Log)", throwing = "e")
    public void getAuthExceptionLog(JoinPoint joinPoint, Exception e) {
        Object[] args = joinPoint.getArgs();
        Signature signature = joinPoint.getSignature();
        String className = signature.getDeclaringTypeName();
        String methodName = signature.getName();
        log.error("==== Exception Start ====");
        log.error("in {}.{}()", className, methodName);
        log.error("param:{}", args);
        log.error(e.getMessage());
        log.error("==== Exception End ======");
    }

}
